import { setToken, getToken, removeToken } from '@/utils/storage'
import { reqGetUserInfo } from '@/api/user'
const state = {
  tokenInfo: getToken(),
  userInfo: ''
}

const mutations = {
  setToken(state, payLoad) {
    state.tokenInfo = payLoad
    setToken(payLoad)
    // localStorage.setItem(TOKEN_KEY, JSON.stringify(payLoad))
  },
  removeToken(state) {
    state.tokenInfo = ''
    removeToken()
  },
  setUserInfo(state, payLoad) {
    state.userInfo = payLoad
  }
}

const actions = {
  async getUserInfo({ commit }) {
    try {
      const res = await reqGetUserInfo()
      commit('setUserInfo', res.data.data)
    } catch (error) {
      return console.dir(error)
    }
  }
}

const getters = {}

export default {
  state,
  mutations,
  actions,
  getters,
  namespaced: true
}
